<template>
  <div>
    <el-dialog
      v-if="editFormVisible"
      :title="title"
      :visible.sync="editFormVisible"
      width="50%"
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" label-width="120px" :model="editForm" :rules="rules">
        <el-form-item label="logo" prop="logo">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="#"
            accept="image/jpeg,image/png,image/jpg"
            list-type="picture-card"
            :before-upload="onBeforeUploadImage"
            :http-request="UploadImage"
            :on-change="fileChange"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">
              点击上传
            </el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/jpeg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">
          取消
        </el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      editFormVisible: [],
      fileList: [],
      logo: ''
    }
  },
  methods: {
    onBeforeUploadImage (file) {
      const isIMAGE = file.type === 'image/jpeg' || 'image/jpg' || 'image/png'
      const isLt1M = file.size / 1024 / 1024 < 1
      if (!isIMAGE) {
        this.$message.error('上传文件只能是图片格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 1MB!')
      }
      return isIMAGE && isLt1M
    },
    UploadImage (param) {
      const formData = new FormData()
      formData.append('file', param.file)
      // eslint-disable-next-line no-undef
      axios.post('/upload/setFileUpload', formData)
        .then((res) => {
          this.$message.success('上传成功！')
          param.onSuccess()
          // this.tableData = []
        })
        .catch(() => {
          this.$message.error('上传失败！')
        })
      // UploadImageApi(formData).then((response) => {
      // // console.log('上传图片成功')
      //   param.onSuccess() // 上传成功的图片会显示绿色的对勾
      //   // 但是我们上传成功了图片， fileList 里面的值却没有改变，还好有on-change指令可以使用
      // }).catch((response) => {
      //   // console.log('图片上传失败')
      //   param.onError()
      // })
    },
    fileChange (file) {
      this.$refs.upload.clearFiles() // 清除文件对象
      this.logo = file.raw // 取出上传文件的对象，在其它地方也可以使用
      this.fileList = [{ name: file.name, url: file.url }] // 重新手动赋值filstList， 免得自定义上传成功了, 而fileList并没有动态改变， 这样每次都是上传一个对象
    }
  }
}
</script>
<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 18px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
